home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
DJGPP
/
DJLSR111.ZIP
/
libsrc
/
c
/
gen
/
sleep.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-01
|
936b
|
49 lines
/*
* Copyright (C) 1993 Chris Boucher, ccb@soton.ac.uk.
*
* This software may be used freely so long as this copyright notice is
* left intact. There is no warranty on the software.
*/
#include <sys/time.h>
sleep(unsigned int seconds)
{
struct timeval a, b, r;
gettimeofday(&a, NULL);
while (1) {
gettimeofday(&b, NULL);
etime(a, b, &r);
if (r.tv_sec >= (long)seconds)
return 0;
}
}
usleep(unsigned int useconds)
{
struct timeval a, b, r;
gettimeofday(&a, NULL);
while (1) {
gettimeofday(&b, NULL);
etime(a, b, &r);
if (((r.tv_sec * 1000000) + r.tv_usec) >= (long)useconds)
return 0;
}
}
static void etime(struct timeval a, struct timeval b, struct timeval *r)
{
r->tv_sec = b.tv_sec - a.tv_sec;
r->tv_usec = b.tv_usec - a.tv_usec;
if (r->tv_usec < 0) {
long t = 1 - (r->tv_usec / 1000000);
r->tv_sec -= t;
r->tv_usec += t * 1000000;
}
}